/**
 * 附体 编辑页
 *
 * 作者 ganjun
 * 日期 2024-11-20 17:18:55
 */
layui.use(['jquery','febs', 'form', 'validate', 'fileUpload'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        fileUpload = layui.fileUpload,
        $view = $('#modelAttach-edit'),

        $source = $view.find('#sourceSelect'),
        $attachType = $view.find('#attachTypeSelect'),

        laydate = layui.laydate,
        validate = layui.validate;

    form.verify(validate);
    form.render();
    initFormValue();

    initDictItemSelect();



    function initFormValue() {
        if (modelAttachData) {
            form.val("modelAttach-edit-form", {
                "id": modelAttachData.id,
                "attachCode": modelAttachData.attachCode,
                "projectCode": modelAttachData.projectCode,
                "attachType": modelAttachData.attachType,
                "attachTypeName": modelAttachData.attachTypeName,
                "attachName": modelAttachData.attachName,
                "source": modelAttachData.source,
                "sourceName": modelAttachData.sourceName,
                "status": modelAttachData.status,
                "statusName": modelAttachData.statusName,
                "demandEnddate": modelAttachData.demandEnddate,
                "actuallyStartdate": modelAttachData.actuallyStartdate,
                "actuallyEnddate": modelAttachData.actuallyEnddate,
                "origAttachCode": modelAttachData.origAttachCode,
                "memo": modelAttachData.memo,
            });
        }

        if(ftNo){
            form.val("modelAttach-edit-form", {
                "attachCode": ftNo,
            });
        }

        if(modelAttachData && modelAttachData.source){
            changeSource(modelAttachData.source.toString());
        }

    }

    function initDictItemSelect() {
        if(modelAttachData){
            //来源
            febs.initDictItemSelect($source, 'MODEL_ATTACH_SOURCE', modelAttachData.source, null);
            //附体类型
            febs.initDictItemSelect($attachType, 'MODEL_ATTACH_TYPE', modelAttachData.attachType, null);
        }else{
            //来源
            febs.initDictItemSelect($source, 'MODEL_ATTACH_SOURCE', null, null);
            //附体类型
            febs.initDictItemSelect($attachType, 'MODEL_ATTACH_TYPE', null, null);
        }
    }

    form.on('select(sourceSelect)', function (data) {
        var val = data.value;
        changeSource(val);
    });

    function changeSource(val){
        if(val === '2'){
            $('#origAttachCodeDiv').show();
        }else{
            $('#origAttachCodeDiv').hide();
        }
    }

    form.on('submit(modelAttach-edit-form-submit)', function (data) {
        addOrUpdate(data.field);
        return false;
    });

    var fileUploadDiv = fileUpload.render({
        elem: '#fileUpload',
        url: ctx + 'upload/file',
        type: 2,//0-图片，1-视频，2-其他
        maxNum:5,
        defaultData: getFileList(),
        done: function (res) {
        },
    });

    function addOrUpdate(data) {
        var params = {};
        params.modelAttach = data;
        params.fileList = getFileData();
        if(data && data.source === '2'){
            if(data.origAttachCode == null || data.origAttachCode == ''){
                febs.alert.warn('已有附体，原附体编号不能为空');
                return;
            }
        }
        febs.modal.confirm("保存提示！","是否保存？",function (index){
            data.projectName = $view.find("select[name='projectCode']").find("option:selected").text();
            if (data.id && data.id.length > 0) {
                febs.postArray(ctx + 'modelAttach/update', params, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                    var currentUrl = window.location.hash.replace('#', '');
                    febs.view.tab.del(currentUrl);
                    // febs.view.tab.change("/modelAttach");
                    $('#febs-modelAttach').find('#query').trigger('click');

                });
            } else {
                febs.postArray(ctx + 'modelAttach', params, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    var currentUrl = window.location.hash.replace('#', '');
                    febs.view.tab.del(currentUrl);
                    // febs.view.tab.change("/modelAttach");
                    $('#febs-modelAttach').find('#query').trigger('click');
                });
            }
        });
    }

    function getFileList() {
        if (attachs && attachs.length>0) {
            return  attachs;
        }else {
            return  [];
        }
    }

    /**
     * 获取附件
     * @returns {*[]}
     */
    function getFileData(){
        var fileDatas = $view.find(".viewFile");
        var fileList = [];
        $.each(fileDatas,function (index, item){
            var data = {
                filePath: item.dataset.filepath,
                fileName: item.dataset.filename,
            }
            fileList.push(data);
        })
        return fileList;
    }

    laydate.render({
        elem: "#demandEnddate",
        type: 'datetime',
        format: 'yyyy-MM-dd HH:mm',
    });

    $('#reset').click(function () {
        closeCur();
    });

    function closeCur(){
        var currentUrl = window.location.hash.replace('#', '');
        febs.view.tab.del(currentUrl);
        // febs.view.tab.change("/modelAttach");
        $('#febs-modelAttach').find('#query').trigger('click');
    }
});